// pages/template/template.js
const templateAPI = require('../../api/template.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    templateList: null
  },
  templateItemClick: function(event){
    // wx.navigateTo({
    //   url:'../detail/detail?incident='+JSON.stringify({
    //     id: 281754149608872
    //   })
    // })
    // return;
    let templateInfo= event.target.dataset;
    wx.navigateTo({
      url: '../launch/launch?template=' + JSON.stringify(templateInfo),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: 'Incident Template List'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.navigateTo({
    //   url: '../launch/launch?template=',
    // })
    // return;
    let auth = app.globalData.authentication;
    const that = this;
    if (typeof auth !=="string") {
      wx.navigateTo({
        url: "../index/index"
      })
      return;
    }
    //TODO remove these two lines
    console.log('auth: ' + auth);
    that.setData({
      templateList: [{ "id": 9006928671805727, "name": "(Sprint 26)Power Outage Notification", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1510130968314, "lastModifiedDate": 1510138052876, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 9006928671805747, "name": "(Sprint 27)IC Custom Support", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 884011643701506, "lastModifiedName": "Albert Luo Multiple", "createdDate": 1510138483048, "lastModifiedDate": 1511492872963, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281754149588371, "name": "(Sprint 30)Car Accident", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1513756859168, "lastModifiedDate": 1517379766572, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281754149588372, "name": "(Sprint 30)Car Accident-Firefighter", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1513765168382, "lastModifiedDate": 1517379781813, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281754149593226, "name": "(Sprint 31)CAP RSS Message", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1514948952382, "lastModifiedDate": 1517186645554, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281754149605128, "name": "(Sprint 31)CAP RSS Message-Token", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1517397478721, "lastModifiedDate": 1517411567756, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281754149600221, "name": "(Sprint 33) Split notification", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1516343195172, "lastModifiedDate": 1517398091808, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281754149600232, "name": "(Sprint 33) Split notification-ContactReadOnly", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1516346761427, "lastModifiedDate": 1516346898807, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281616710674166, "name": "(Sprint 35) Dynamic Location API", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1521024599189, "lastModifiedDate": 1521025007719, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281616710674325, "name": "(Sprint 37) CMDB Lookup", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 884011643701506, "lastModifiedName": "Albert Luo Multiple", "createdDate": 1521032063189, "lastModifiedDate": 1522851532844, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281616710683590, "name": "(Sprint 38) CMDB Lookup - Existing Object", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 884011643701506, "createdName": "Albert Luo Multiple", "lastModifiedId": 884011643701506, "lastModifiedName": "Albert Luo Multiple", "createdDate": 1522844528716, "lastModifiedDate": 1522852739160, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 9007066110762793, "name": "(sprint 28) UI changes for contact part and hyperlink", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 884011643701536, "createdName": "Nakita Sun", "lastModifiedId": 884011643701536, "lastModifiedName": "Nakita Sun", "createdDate": 1511345357678, "lastModifiedDate": 1511359193055, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281616710698135, "name": "(sprint 40 demo)location: geo point", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 281616710637226, "createdName": "Aria shu", "lastModifiedId": 281616710637226, "lastModifiedName": "Aria shu", "createdDate": 1525747694972, "lastModifiedDate": 1525875570127, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 444206992619946, "name": "El Paso Interstate Shutdown Warning", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 448605039100303, "createdName": "SmartConference1 wzhang", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1493107689800, "lastModifiedDate": 1502763377086, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 444206992623299, "name": "Fire Alarm", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992592324, "createdName": "Incident Admin&Operator", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1499270471589, "lastModifiedDate": 1499295039454, "organizationId": 888409690211392, "resourceBundleId": 444206992590732, "category": { "id": 444206992591505, "name": "Incident Zone" } }, { "id": 444206992635420, "name": "Hyper care issue found!", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1504006649137, "lastModifiedDate": 1505309239181, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 884011643722135, "name": "IC Custom Support-new", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 884011643701506, "lastModifiedName": "Albert Luo", "createdDate": 1495098126325, "lastModifiedDate": 1501630022491, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 444206992629134, "name": "Msg Template", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1502763330882, "lastModifiedDate": 1502791538208, "organizationId": 888409690211392, "resourceBundleId": 888409690211649, "category": { "id": 444206992591516, "name": "Translation" } }, { "id": 884011643742843, "name": "Team Availability Polling", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 444206992591625, "createdName": "Albert Luo", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1505309870643, "lastModifiedDate": 1505309905416, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 444206992619948, "name": "Terrorist shooting", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 448605039100303, "createdName": "SmartConference1 wzhang", "lastModifiedId": 444206992591625, "lastModifiedName": "Albert Luo", "createdDate": 1493107975335, "lastModifiedDate": 1505308749967, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }, { "id": 281754149631901, "name": "location geo point", "templateStatus": "Live", "accountId": 448605039100272, "status": "A", "createdId": 281616710637226, "createdName": "Aria shu", "lastModifiedId": 281616710637226, "lastModifiedName": "Aria shu", "createdDate": 1525703910167, "lastModifiedDate": 1525703910167, "organizationId": 888409690211392, "resourceBundleId": 888409690211649 }]
    })
    return;
    if (this.data.templateList!==null) {
      return;
    }
    wx.showLoading({
      title: 'Loading...',
      mask: true
    });
    const doneCallBack = () => {
      wx.hideLoading();
    }
    templateAPI.getAllTemplates(res => {
      that.setData({
        templateList: res.data.page.data
      })
      doneCallBack();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})